using System;
using System.Collections.Generic;
using System.Text;
using GuavaData;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel.Design;
using System.ComponentModel;

namespace GuavaLib
{
    [TypeConverter(typeof(GLookupTableColumnConverter)), Serializable()]
    public class GLookupTableColumn : Object, Guava
    {
        string columnName;

        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }
        GuavaData.Domain.Domain gdd;
        public GLookupTableColumn()
        {
            this.columnName = "newColumn";
            gdd = new GuavaData.Domain.String();
        }
        public GLookupTableColumn(string columnName, GuavaData.Domain.Domain dom)
        {
            this.columnName = columnName;
            this.gdd = dom;
        }

        #region Guava Members

        [Browsable(false)]
        public LaunchType LType
        {
            get { return LaunchType.NA; }
        }
        [Browsable(false)]
        public ControlType CType
        {
            get { return ControlType.Attribute; }
        }
        [Browsable(false)]
        public string GText
        {
            get { return null; }
        }
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string GName
        {
            get
            {
                return columnName;
            }
            set
            {
                columnName = value;
            }
        }
        [Browsable(false)]
        public System.Drawing.Point GLocation
        {
            get { return new System.Drawing.Point(); }
        }
        [Browsable(false)]
        public System.Drawing.Size GSize
        {
            get { return new System.Drawing.Size(); }
        }
        [Browsable(false)]
        public List<Guava> GComponents
        {
            get { return new List<Guava>(); }
        }
        public void GBind(string table, int i)
        {
            return;
        }
        [Browsable(false)]
        public string GDefault
        {
            get { return null; }
        }
        [Browsable(false)]
        public string GToolTip
        {
            get { return null; }
        }
        public GuavaData.Domain.Domain GDomain
        {
            get { return gdd; }
            set { gdd = value; }
        }
        public Guava this[string s]
        {
            get { return null; }
        }

        #endregion

        public override string ToString()
        {
            return ColumnName + ": " + gdd.ToString();
        }
    }

    public class GLookupTableColumnConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                GLookupTableColumn gtc = value as GLookupTableColumn;
                System.Reflection.ConstructorInfo ci = typeof(GLookupTableColumn).GetConstructor(new Type[] { typeof(string), typeof(GuavaData.Domain.Domain) });
                return new InstanceDescriptor(ci, new object[] { gtc.GName, gtc.GDomain });
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
